Loads a list of OPC "Classic" items from an XML file and subscribes to them.
The main program:
// This example shows how to load a list of OPC "Classic" items from an XML file and subscribe to them. using System; using System.Threading; using System.Xml; using System.Xml.Serialization; using OpcLabs.BaseLib.Runtime.InteropServices; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace SubscribeFromXml { class Program { static void Main() { ComManagement.Instance.AssureSecurityInitialization(); Console.WriteLine("Loading items from XML file..."); var xmlSerializer = new XmlSerializer(typeof(DAItemGroupArguments[])); var xmlReader = XmlReader.Create("OpcItems.xml", new XmlReaderSettings {IgnoreWhitespace = true}); var argArray = (DAItemGroupArguments[]) (xmlSerializer.Deserialize(xmlReader)); if (!(argArray is null)) { Console.WriteLine(); Console.WriteLine("Arguments loaded:"); foreach (DAItemGroupArguments itemGroupArguments in argArray) Console.WriteLine($" {itemGroupArguments}"); Console.WriteLine(); Console.WriteLine("Subscribing for 30 seconds..."); EasyDAClient.SharedInstance.SubscribeMultipleItems(argArray, (_, eventArgs) => Console.WriteLine( $"[{eventArgs.Arguments.State}] {eventArgs.Arguments.ItemDescriptor.ItemId}: {eventArgs.Vtq}")); Thread.Sleep(30 * 1000); Console.WriteLine(); Console.WriteLine("Unsubscribing..."); EasyDAClient.SharedInstance.UnsubscribeAllItems(); } Console.WriteLine(); Console.WriteLine("Finished."); } } }
Imports System.Threading Imports System.Xml Imports System.Xml.Serialization Imports OpcLabs.BaseLib.Runtime.InteropServices Imports OpcLabs.EasyOpc.DataAccess Imports OpcLabs.EasyOpc.DataAccess.OperationModel Friend Class Program Shared WithEvents _client As New EasyDAClient <MTAThread> ' needed for COM security initialization to succeed Shared Sub Main() ComManagement.Instance.AssureSecurityInitialization() Console.WriteLine("Loading items from XML file...") Dim xmlSerializer = New XmlSerializer(GetType(DAItemGroupArguments())) Dim xmlReader = Xml.XmlReader.Create("OpcItems.xml", New XmlReaderSettings With {.IgnoreWhitespace = True}) Dim argArray = CType(xmlSerializer.Deserialize(xmlReader), DAItemGroupArguments()) If argArray IsNot Nothing Then Console.WriteLine("Subscribing for 30 seconds...") _client.SubscribeMultipleItems(argArray) Thread.Sleep(30 * 1000) Console.WriteLine("Unsubscribing...") _client.UnsubscribeAllItems() End If Console.WriteLine("Finished.") End Sub Private Shared Sub ItemChanged(ByVal sender As Object, ByVal eventArgs As EasyDAItemChangedEventArgs) Handles _client.ItemChanged Console.WriteLine("{0}: {1}", eventArgs.Arguments.ItemDescriptor.ItemId, eventArgs.Vtq) End Sub End Class
The XML file with list of items:
<?xml version="1.0" encoding="utf-8"?> <!-- This file specifies the OPC items to be subscribed to, and their associated state objects (integers, in this case). --> <ArrayOfDAItemGroupArguments xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <DAItemGroupArguments> <State xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.OperationModel"> <TypeFullName xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">System.Int32</TypeFullName> <Instance xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">1</Instance> </State> <ServerDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess.OperationModel"> <UrlString xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib">opcda:OPCLabs.KitServer.2</UrlString> </ServerDescriptor> <ItemDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess.OperationModel"> <NodeId xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc">Simulation.Random</NodeId> </ItemDescriptor> <GroupParameters xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess.OperationModel"> <RequestedUpdateRate xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess">1000</RequestedUpdateRate> <PercentDeadband xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess">0.0</PercentDeadband> </GroupParameters> </DAItemGroupArguments> <DAItemGroupArguments> <State xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.OperationModel"> <TypeFullName xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">System.Int32</TypeFullName> <Instance xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib.Xml">2</Instance> </State> <ServerDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess.OperationModel"> <UrlString xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.BaseLib">opcda:OPCLabs.KitServer.2</UrlString> </ServerDescriptor> <ItemDescriptor xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess.OperationModel"> <NodeId xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc">Simulation.Ramp (10 s)</NodeId> </ItemDescriptor> <GroupParameters xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess.OperationModel"> <RequestedUpdateRate xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess">1000</RequestedUpdateRate> <PercentDeadband xmlns="http://schemas.datacontract.org/2004/07/OpcLabs.EasyOpc.DataAccess">0.0</PercentDeadband> </GroupParameters> </DAItemGroupArguments> </ArrayOfDAItemGroupArguments>
Copyright © 2004-2023 CODE Consulting and Development, s.r.o., Plzen. All rights reserved. Web page: www.opclabs.com
Send Documentation Feedback. Resources: Knowledge Base. Technical support: Online Forums, FAQ.